﻿using D.Tool.CodeBuild.Models; 
using CozeDomain.IRepositories;
using CozeDomain.Entities;
using D.UtilFreesqlOrm;

namespace CozeDomain.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class SysCodeBuilderRepository : BaseRepository<SysCodeBuilderEntity>, ISysCodeBuilderRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="freeSql"></param>
        /// <param name="uow"></param>
        public SysCodeBuilderRepository(IFreeSql db) : base(db)
        {

        }

        /// <summary>
        /// 获取库
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> GetDbList()
        {
            return _db.DbFirst.GetDatabases();
        }

        /// <summary>
        /// 获取表
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public IEnumerable<TablesInput> GetTableList(string dbName)
        {
            var list = _db.DbFirst.GetTablesByDatabase(new string[] { dbName });
            var tablesInputs = new List<TablesInput>();
            foreach (var item in list)
            {
                tablesInputs.Add(new TablesInput()
                {
                    Comment = item.Comment,
                    Id = item.Id,
                    Name = item.Name,
                    Columns = item.Columns.Select(m => new ColumnsInput()
                    {
                        Name = m.Name,
                        Coment = m.Coment,
                        DbTypeTextFull = m.DbTypeTextFull,
                        DefaultValue = m.DefaultValue,
                        IsIdentity = m.IsIdentity,
                        IsNullable = m.IsNullable,
                        IsPrimary = m.IsPrimary,
                        MaxLength = m.MaxLength,
                        FiledType = m.CsType.FullName,
                    }).ToList()
                });
            }
            return tablesInputs;
        }

    }
}
